#include "Mesh.h"

/*-----------------------------------------------------------------------------------------------*/
Mesh::Mesh( std::string name,
           unsigned short vertexCount, SuperVertex* vertices, 
           unsigned short indexCount, unsigned int* indices, 
           PRIM_TYPE primType, unsigned short primCount ):

Entity(name),
m_vertexCount(vertexCount), m_pVertices(vertices),
m_indexCount(indexCount), m_pIndices(indices),
m_primitiveType(primType), m_primCount(primCount)
{
   this->setDefaultMaterial();
   createBoundingBox();
}
/*-----------------------------------------------------------------------------------------------*/
void Mesh::draw( Renderer* renderer )
{
   if ( isVisible() )
   {
      renderer->setMaterials(&m_material);
      
      if (m_pTexture.use_count() != 0L)
      {
         renderer->bindTexture(m_pTexture);
      }
      else
      {
         renderer->unbindTexture();
      }

      renderer->DrawElements(m_vertexCount, m_pVertices, m_indexCount, m_pIndices, m_primCount, m_primitiveType);
   }

   //m_boundingBox->draw(renderer);
}
/*-----------------------------------------------------------------------------------------------*/
bool Mesh::setVertexUV( int index, float u, float v )
{
   if( !m_pTexture )
   {
      return false;
   }

   m_pVertices[index].u = u;
   m_pVertices[index].v = v;

   return true;
}
/*-----------------------------------------------------------------------------------------------*/
void Mesh::setVertexColor(int index, float r, float g, float b, float a)
{
   m_pVertices[index].color = D3DXCOLOR(r,g,b,a);
}

/*-----------------------------------------------------------------------------------------------*/
SuperVertex* Mesh::getVertices() const
{
   return m_pVertices;
}
/*-----------------------------------------------------------------------------------------------*/
size_t Mesh::getNumVertices() const
{
   return m_vertexCount;
}
/*-----------------------------------------------------------------------------------------------*/
void Mesh::setDefaultMaterial()
{
   m_material.Diffuse = D3DXCOLOR(1,1,1,1);
   m_material.Ambient = D3DXCOLOR(1,1,1,1); 
   m_material.Emissive = D3DXCOLOR(0,0,0,0);
   m_material.Specular = D3DXCOLOR(0,0,0,0);
   m_material.Power = 0;
}
/*-----------------------------------------------------------------------------------------------*/
void Mesh::createBoundingBox()
{
   Vector3 max, min;

   //get the maximum and minimum values
   for (size_t i = 0; i < m_vertexCount; ++i)
   {
      const float x = m_pVertices[i].x;
      const float y = m_pVertices[i].y;
      const float z = m_pVertices[i].z;
    
      //if its the first time we assign defaut values and continue
      if (i == 0)
      {
         max.x = x, min.x = x;
         max.y = y, min.y = y;
         max.z = z, min.z = z;
   
         continue;
      }
   
      if (x > max.x) max.x = x; 
      if (x < min.x) min.x = x;
   
      if (y > max.y) max.y = y;
      if (y < min.y) min.y = y;
   
      if (z > max.z) max.z = z;
      if (z < min.z) min.z = z;
   }

   //create the bbox
   m_boundingBox = new BoundingBox(max, min);
}

